class Admin::PrintProductFieldController < Admin::BaseController
  
  active_scaffold :print_product_field do |config|
      config.label = "产品字段管理"
      config.columns = [:product, :is_required, :name, :field_name, :column_type, :position, :description, :values]
      config.create.columns = [:product, :is_required, :name, :field_name, :column_type, :position, :description]
      config.list.columns.exclude :description, :values
      list.sorting = {:name => 'ASC'}
      
      columns[:product].label = "产品名称"
      columns[:is_required].label = "必填"
      columns[:name].label = "名称"
      columns[:field_name].label = "字段名"
      columns[:column_type].label = "类型"
      columns[:position].label = "位置"
      columns[:description].label = "描述"
      columns[:values].label = "可能值"
      
  end
  
  auto_complete_for :print_product, :name, :limit => 20, :order => 'name ASC'
  
  protected
  def before_create_save(record)
    set_product(record)
  end
  
  def before_update_save(record)
    set_product(record)
  end
  
  private
  def set_product(record)
    product_name = params[:print_product][:name]
    product = PrintProduct.find_by_name(product_name)
    record.print_product_id = product.id
  end
end
